package com.szly.phm.common.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szly.phm.common.entity.dto.FaFault.FaultRecordPhenomenonDTO;
import com.szly.phm.common.entity.dto.faRepair.*;
import com.szly.phm.common.entity.dto.user.LoginUserDTO;
import com.szly.phm.common.entity.po.*;
import com.szly.phm.common.entity.result.CodeStatusEnum;
import com.szly.phm.common.entity.result.PageResult;
import com.szly.phm.common.entity.result.RootResponse;
import com.szly.phm.common.entity.vo.Fault.FaultRecordPhenomenonVO;
import com.szly.phm.common.entity.vo.faRepair.*;
import com.szly.phm.common.mapper.*;
import com.szly.phm.common.service.IRepairService;
import com.szly.phm.common.utils.BeanCopyUtils;
import com.szly.phm.common.utils.HandleUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * phm
 *
 * @Models:
 * *************************************
 *      FaProgrammeMeasures
 *      FaRepairEquipment
 *      FaRepairMeasures
 *      FaRepairProgramme
 *      FaRepairRecord
 *      FaTechnicalDocumentation
 * *************************************
 * @author: git.jas0nch
 * date: 2021-10-23
 */

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RepairServiceImpl implements IRepairService {

    private final SysUserMapper sysUserMapper;

    private final FaProgrammeMeasuresMapper faProgrammeMeasuresMapper;

    private final FaRepairEquipmentMapper faRepairEquipmentMapper;

    private final FaRepairMeasuresMapper faRepairMeasuresMapper;

    private final FaRepairProgrammeMapper faRepairProgrammeMapper;

    private final FaRepairRecordMapper faRepairRecordMapper;

    private final FaTechnicalDocumentationMapper faTechnicalDocumentationMapper;

    private final FaFaultRecordMapper faFaultRecordMapper;

    private final FaPhenomenonRecordMapper faPhenomenonRecordMapper;
    // radar
    private final RaTreeMapper raTreeMapper;

    private final RaRadarMapper raRadarMapper;

    private final FiRadarTypeMapper fiRadarTypeMapper;

    private final RaSystemMapper raSystemMapper;

    private final RaExtensionMapper raExtensionMapper;

    private final RaKeyPartsModelMapper raKeyPartsModelMapper;

    private final FaRepairWaysMapper faRepairWaysMapper;

    private final FaRepairDeviceMapper faRepairDeviceMapper;

    private final FaEnsureMapper faEnsureMapper;
    private final SysMessageMapper  sysMessageMapper;

    private final FaDeviceMeasuresMapper faDeviceMeasuresMapper;
    private final FaEquipmentMeasuresMapper faEquipmentMeasuresMapper;


    /**
     * @param repairWaysPageDTO:
     * @return
     */
    @Override
    public RootResponse<PageResult<FaRepairWaysVO>> queryFaRepairWaysPage(FaRepairWaysPageDTO repairWaysPageDTO) {
        var wrapper = new LambdaQueryWrapper<FaRepairWays>();
        wrapper.eq(FaRepairWays::getIsSoftDelete, false);
        // fuzzy query 模糊查询；
        if (repairWaysPageDTO.getName()!=null){
            // if not null, then get the specially query by the name;
            wrapper.like(FaRepairWays::getName,repairWaysPageDTO.getName());
        }
        //
        wrapper.orderByDesc(FaRepairWays::getCreateDateTime);
        // page helper
        PageHelper.startPage(repairWaysPageDTO.getPageNum(), repairWaysPageDTO.getPageSize());
        var RepairWayss = faRepairWaysMapper.selectList(wrapper);
        var pageInfo = PageInfo.of(RepairWayss);
        var pageList = pageInfo.getList();
        System.out.println(pageList);
        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));
    }

    @Override
    public RootResponse createFaRepairWays(CreateFaRepairWaysDTO createRepairWaysDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<FaRepairWays>();
        // 查重：
        wrapper.eq(FaRepairWays::getName, createRepairWaysDTO.getName());
        wrapper.eq(FaRepairWays::getIsSoftDelete, false);
        if (faRepairWaysMapper.selectOne(wrapper) != null) {
            return RootResponse.error("维修方式已存在，请勿重复添加");
        }
        //
        var faRepairWays = new FaRepairWays();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createRepairWaysDTO, faRepairWays);
        faRepairWays.setId(HandleUtils.UUID32());
        faRepairWays.setCreator(userDTO.getUsername());
        faRepairWays.setCreateDateTime(new Date());
        faRepairWays.setIsSoftDelete(false);
        int i = faRepairWaysMapper.insert(faRepairWays);
        return i == 1 ? RootResponse.success():RootResponse.error();
    }

    @Override
    public RootResponse updateFaRepairWays(UpdateFaRepairWaysDTO updateRepairWaysDTO, LoginUserDTO userDTO) {
        var wrapper1 = new LambdaQueryWrapper<FaRepairWays>();
        wrapper1.eq(FaRepairWays::getName, updateRepairWaysDTO.getName());
        wrapper1.eq(FaRepairWays::getIsSoftDelete, false);
        var faRepairWays1=faRepairWaysMapper.selectOne(wrapper1);
        //
        if(faRepairWays1!=null){
            if (!faRepairWays1.getId().equals(updateRepairWaysDTO.getId())) {
//            var FaTaskLevel2 = baseMapper.selectOne(wrapper1);
                return RootResponse.error("维修方式已存在，请勿重复添加");

            }
        }

        // the old data:
        var faRepairWays_old = faRepairWaysMapper.selectById(updateRepairWaysDTO.getId());
        var faRepairWays=new FaRepairWays();
        BeanCopyUtils.copy(updateRepairWaysDTO, faRepairWays);
        faRepairWays.setUpdater(userDTO.getUsername());
        faRepairWays.setUpdateDateTime(new Date());
        int i = faRepairWaysMapper.updateById(faRepairWays);
        return i == 1 ? RootResponse.success(faRepairWays_old) : RootResponse.error();
    }

    @Override
    public RootResponse deleteFaRepairWays(String repairWaysId, LoginUserDTO userDTO) {
        // 判断是否在 维修方案中存在，如果存在则不能删除
        LambdaQueryWrapper<FaRepairProgramme> faRepairProgrammeQueryWrapper = new LambdaQueryWrapper<>();
        faRepairProgrammeQueryWrapper.eq(FaRepairProgramme::getIsSoftDelete,false);
        faRepairProgrammeQueryWrapper.eq(FaRepairProgramme::getRepairWayId,repairWaysId);
        if (faRepairProgrammeMapper.selectList(faRepairProgrammeQueryWrapper).size()>0){
            return RootResponse.error("该维修方式已在维修方案中，请勿删除");
        }

        var faRepairWays_old = faRepairWaysMapper.selectById(repairWaysId);
        var queryWrapper = new LambdaQueryWrapper<FaRepairWays>();
        queryWrapper.eq(FaRepairWays::getId, repairWaysId);
        queryWrapper.eq(FaRepairWays::getIsSoftDelete, false);
        var faRepairWays = faRepairWaysMapper.selectOne(queryWrapper);
        if (faRepairWays== null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        faRepairWays.setIsSoftDelete(true);
        faRepairWays.setUpdateDateTime(new Date());
        faRepairWays.setUpdater(userDTO.getUsername());
        faRepairWaysMapper.updateById(faRepairWays);
        return RootResponse.success(faRepairWays_old);
    }


    /**
     *
     * @param faProgrammeMeasuresPageDTO:
     * @return
     */
    @Override
    public RootResponse<PageResult<FaProgrammeMeasuresVO>> queryFaProgrammeMeasuresPage(FaProgrammeMeasuresPageDTO faProgrammeMeasuresPageDTO) {
//        var wrapper = new LambdaQueryWrapper<FaProgrammeMeasures>();
//        //
//        wrapper.eq(FaProgrammeMeasures::getIsSoftDelete, false);
//        // fuzzy query 模糊查询；
//        if (faProgrammeMeasuresPageDTO.getId()!=null){
//            // if not null, then get the specially query by the name;
//            wrapper.like(FaProgrammeMeasures::getId,faProgrammeMeasuresPageDTO.getId());
//        }
//        //
//        wrapper.orderByDesc(FaProgrammeMeasures::getCreateDateTime, FaProgrammeMeasures::getId);
        // page helper
        PageHelper.startPage(faProgrammeMeasuresPageDTO.getPageNum(), faProgrammeMeasuresPageDTO.getPageSize());
        var taskLevels = faProgrammeMeasuresMapper.selectProgram(faProgrammeMeasuresPageDTO);
        var pageInfo = PageInfo.of(taskLevels);
        var pageList = pageInfo.getList();
        System.out.println(pageList);//
        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse createFaProgrammeMeasures(CreateFaProgrammeMeasuresDTO createFaProgrammeMeasuresDTO, LoginUserDTO userDTO) {
        //
        var wrapper = new LambdaQueryWrapper<FaProgrammeMeasures>();
        wrapper.eq(FaProgrammeMeasures::getName, createFaProgrammeMeasuresDTO.getName());
        wrapper.eq(FaProgrammeMeasures::getProgrammeId, createFaProgrammeMeasuresDTO.getProgrammeId());
        wrapper.eq(FaProgrammeMeasures::getIsSoftDelete, false);
//        var FaProgrammeMeasures = FaProgrammeMeasuresMapper.selectOne(wrapper);
        if (faProgrammeMeasuresMapper.selectOne(wrapper) != null) {
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(), "方案明细已存在，请勿重复添加");
        }
        //
        var faProgrammeMeasures = new FaProgrammeMeasures();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createFaProgrammeMeasuresDTO, faProgrammeMeasures);
        faProgrammeMeasures.setId(HandleUtils.UUID32());
        faProgrammeMeasures.setCreator(userDTO.getUsername());
        faProgrammeMeasures.setCreateDateTime(new Date());
        faProgrammeMeasures.setIsSoftDelete(false);
        int i = faProgrammeMeasuresMapper.insert(faProgrammeMeasures);
        return i == 1 ? RootResponse.success():RootResponse.error();
    }

    @Override
    public RootResponse updateFaProgrammeMeasures(UpdateFaProgrammeMeasuresDTO updateFaProgrammeMeasuresDTO, LoginUserDTO userDTO) {
        // 没有name 字段，更新的时候，不做查重操作。
        var wrapper1 = new LambdaQueryWrapper<FaProgrammeMeasures>();
        wrapper1.eq(FaProgrammeMeasures::getName, updateFaProgrammeMeasuresDTO.getName());
        wrapper1.eq(FaProgrammeMeasures::getProgrammeId, updateFaProgrammeMeasuresDTO.getProgrammeId());
        wrapper1.eq(FaProgrammeMeasures::getIsSoftDelete, false);
        var faProgrammeMeasures1 = faProgrammeMeasuresMapper.selectOne(wrapper1);
        if (faProgrammeMeasures1!=null){
            if (!faProgrammeMeasures1.getId().equals(updateFaProgrammeMeasuresDTO.getId())) {
//            var FaProgrammeMeasures2 = baseMapper.selectOne(wrapper1);
                return RootResponse.error("方案明细名称已存在，请勿重复添加");
            }
        }
        // the old data:
        var faProgrammeMeasures_old = faProgrammeMeasuresMapper.selectById(updateFaProgrammeMeasuresDTO.getId());
        var faProgrammeMeasures=new FaProgrammeMeasures();
        BeanCopyUtils.copy(updateFaProgrammeMeasuresDTO, faProgrammeMeasures);
        faProgrammeMeasures.setUpdater(userDTO.getUsername());
        faProgrammeMeasures.setUpdateDateTime(new Date());
        int i = faProgrammeMeasuresMapper.updateById(faProgrammeMeasures);
//        int i=1;
        return i == 1 ? RootResponse.success(faProgrammeMeasures_old) : RootResponse.error();
    }

    @Override
    public RootResponse deleteFaProgrammeMeasures(String id, LoginUserDTO userDTO) {
        // 删除前，先判断是否在维修记录中存在。
        LambdaQueryWrapper<FaRepairRecord> faRepairRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        faRepairRecordLambdaQueryWrapper.eq(FaRepairRecord::getIsSoftDelete, false);
        faRepairRecordLambdaQueryWrapper.eq(FaRepairRecord::getRepairProgramId, faProgrammeMeasuresMapper.selectById(id).getProgrammeId());
        if (faRepairRecordMapper.selectList(faRepairRecordLambdaQueryWrapper).size()>0){
            return RootResponse.error("方案明细所在的维修方案可能在维修记录中，或者正被故障记录使用，请勿删除");
        }
        //
        var faEnsurePlan_old = faProgrammeMeasuresMapper.selectById(id);
        var wrapper = new LambdaQueryWrapper<FaProgrammeMeasures>();
        wrapper.eq(FaProgrammeMeasures::getId, id);
        wrapper.eq(FaProgrammeMeasures::getIsSoftDelete, false);
        var faProgrammeMeasures = faProgrammeMeasuresMapper.selectOne(wrapper);
        if (faProgrammeMeasures== null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        faProgrammeMeasures.setIsSoftDelete(true);
        faProgrammeMeasures.setUpdateDateTime(new Date());
        faProgrammeMeasures.setUpdater(userDTO.getUsername());
        faProgrammeMeasuresMapper.updateById(faProgrammeMeasures);
        return RootResponse.success(faEnsurePlan_old);
    }


    /**
     * @description : TODO
     * @param faRepairDeviceMeasuresPageDTO
     * @return : com.szly.phm.common.entity.result.RootResponse<com.szly.phm.common.entity.result.PageResult<com.szly.phm.common.entity.vo.faRepair.FaRepairDeviceMeasuresPageVO>>
     * @author : git.jas0nch
     * date : 2022/2/8
     **/
    @Override
    public RootResponse<PageResult<FaRepairDeviceMeasuresPageVO>> queryFaRepairDeviceMeasuresPage(FaRepairDeviceMeasuresPageDTO faRepairDeviceMeasuresPageDTO) {
        PageHelper.startPage(faRepairDeviceMeasuresPageDTO.getPageNum(), faRepairDeviceMeasuresPageDTO.getPageSize());
        //
        LambdaQueryWrapper<FaDeviceMeasures> deviceMeasuresLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deviceMeasuresLambdaQueryWrapper.eq(FaDeviceMeasures::getIsSoftDelete, false);
        deviceMeasuresLambdaQueryWrapper.eq(FaDeviceMeasures::getName, faRepairDeviceMeasuresPageDTO.getName());
        //
        var taskLevels = faDeviceMeasuresMapper.selectList(deviceMeasuresLambdaQueryWrapper);
        var pageInfo = PageInfo.of(taskLevels);
        var pageList = pageInfo.getList();
        System.out.println(pageList);//
        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse createFaRepairDeviceMeasures(CreateFaRepairDeviceMeasuresDTO createFaRepairDeviceMeasuresDTO, LoginUserDTO userDTO) {
        //
        var wrapper = new LambdaQueryWrapper<FaDeviceMeasures>();
        wrapper.eq(FaDeviceMeasures::getName, createFaRepairDeviceMeasuresDTO.getName());
        wrapper.eq(FaDeviceMeasures::getDeviceId, createFaRepairDeviceMeasuresDTO.getDeviceId());
        wrapper.eq(FaDeviceMeasures::getIsSoftDelete, false);
//        var FaDeviceMeasures = FaDeviceMeasuresMapper.selectOne(wrapper);
        if (faDeviceMeasuresMapper.selectOne(wrapper) != null) {
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(), "设备明细已存在，请勿重复添加");
        }
        //
        var faDeviceMeasures = new FaDeviceMeasures();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createFaRepairDeviceMeasuresDTO, faDeviceMeasures);
        faDeviceMeasures.setId(HandleUtils.UUID32());
        faDeviceMeasures.setCreator(userDTO.getUsername());
        faDeviceMeasures.setCreateDateTime(new Date());
        faDeviceMeasures.setIsSoftDelete(false);
        int i = faDeviceMeasuresMapper.insert(faDeviceMeasures);
        return i == 1 ? RootResponse.success():RootResponse.error();
    }

    @Override
    public RootResponse updateFaRepairDeviceMeasures(UpdateFaRepairDeviceMeasuresDTO updateFaRepairDeviceMeasuresDTO, LoginUserDTO userDTO) {
        // 没有name 字段，更新的时候，不做查重操作。
        var wrapper1 = new LambdaQueryWrapper<FaDeviceMeasures>();
        wrapper1.eq(FaDeviceMeasures::getName, updateFaRepairDeviceMeasuresDTO.getName());
        wrapper1.eq(FaDeviceMeasures::getProgrammeId, updateFaRepairDeviceMeasuresDTO.getProgrammeId());
        wrapper1.eq(FaDeviceMeasures::getIsSoftDelete, false);
        var faProgrammeMeasures1 = faDeviceMeasuresMapper.selectOne(wrapper1);
        if (faProgrammeMeasures1!=null){
            if (!faProgrammeMeasures1.getId().equals(updateFaRepairDeviceMeasuresDTO.getId())) {
//            var FaProgrammeMeasures2 = baseMapper.selectOne(wrapper1);
                return RootResponse.error("设备明细名称已存在，请勿重复添加");
            }
        }
        // the old data:
        var faProgrammeMeasures_old = faProgrammeMeasuresMapper.selectById(updateFaRepairDeviceMeasuresDTO.getId());
        var faProgrammeMeasures=new FaProgrammeMeasures();
        BeanCopyUtils.copy(updateFaRepairDeviceMeasuresDTO, faProgrammeMeasures);
        faProgrammeMeasures.setUpdater(userDTO.getUsername());
        faProgrammeMeasures.setUpdateDateTime(new Date());
        int i = faProgrammeMeasuresMapper.updateById(faProgrammeMeasures);
//        int i=1;
        return i == 1 ? RootResponse.success(faProgrammeMeasures_old) : RootResponse.error();
    }

    @Override
    public RootResponse deleteFaRepairDeviceMeasures(String id, LoginUserDTO userDTO) {
        // 删除前，先判断是否在维修记录中存在。
        LambdaQueryWrapper<FaRepairRecord> faRepairRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        faRepairRecordLambdaQueryWrapper.eq(FaRepairRecord::getIsSoftDelete, false);
        faRepairRecordLambdaQueryWrapper.eq(FaRepairRecord::getRepairProgramId, faProgrammeMeasuresMapper.selectById(id).getProgrammeId());
        if (faRepairRecordMapper.selectList(faRepairRecordLambdaQueryWrapper).size()>0){
            return RootResponse.error("设备明细所在的维修方案可能在维修记录中，或者正被故障记录使用，请勿删除");
        }
        //
        var faEnsurePlan_old = faProgrammeMeasuresMapper.selectById(id);
        var wrapper = new LambdaQueryWrapper<FaProgrammeMeasures>();
        wrapper.eq(FaProgrammeMeasures::getId, id);
        wrapper.eq(FaProgrammeMeasures::getIsSoftDelete, false);
        var faProgrammeMeasures = faProgrammeMeasuresMapper.selectOne(wrapper);
        if (faProgrammeMeasures== null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        faProgrammeMeasures.setIsSoftDelete(true);
        faProgrammeMeasures.setUpdateDateTime(new Date());
        faProgrammeMeasures.setUpdater(userDTO.getUsername());
        faProgrammeMeasuresMapper.updateById(faProgrammeMeasures);
        return RootResponse.success(faEnsurePlan_old);
    }

    /**
     * @description : TODO
     * @param faRepairEquipmentMeasuresPageDTO
     * @return : com.szly.phm.common.entity.result.RootResponse<com.szly.phm.common.entity.result.PageResult<com.szly.phm.common.entity.vo.faRepair.FaRepairEquipmentMeasuresVO>>
     * @author : git.jas0nch
     * date : 2022/2/8
     **/
    @Override
    public RootResponse<PageResult<FaRepairEquipmentMeasuresVO>> queryFaRepairEquipmentMeasuresPage(FaRepairEquipmentMeasuresDTO faRepairEquipmentMeasuresPageDTO) {
        PageHelper.startPage(faRepairEquipmentMeasuresPageDTO.getPageNum(), faRepairEquipmentMeasuresPageDTO.getPageSize());
        //
        LambdaQueryWrapper<FaEquipmentMeasures> faEquipmentMeasuresLambdaQueryWrapper = new LambdaQueryWrapper<>();
        faEquipmentMeasuresLambdaQueryWrapper.eq(FaEquipmentMeasures::getIsSoftDelete, false);
        faEquipmentMeasuresLambdaQueryWrapper.eq(FaEquipmentMeasures::getName, faRepairEquipmentMeasuresPageDTO.getName());
        //
        var taskLevels = faEquipmentMeasuresMapper.selectList(faEquipmentMeasuresLambdaQueryWrapper);
        var pageInfo = PageInfo.of(taskLevels);
        var pageList = pageInfo.getList();
        System.out.println(pageList);//
        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse createFaRepairEquipmentMeasures(CreateFaRepairEquipmentMeasuresDTO createFaRepairEquipmentMeasuresDTO, LoginUserDTO userDTO) {
        //
        var wrapper = new LambdaQueryWrapper<FaEquipmentMeasures>();
        wrapper.eq(FaEquipmentMeasures::getName, createFaRepairEquipmentMeasuresDTO.getName());
        wrapper.eq(FaEquipmentMeasures::getEquipmentId, createFaRepairEquipmentMeasuresDTO.getEquipmentId());
        wrapper.eq(FaEquipmentMeasures::getIsSoftDelete, false);
//        var FaEquipmentMeasures = FaEquipmentMeasuresMapper.selectOne(wrapper);
        if (faEquipmentMeasuresMapper.selectOne(wrapper) != null) {
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(), "器材明细已存在，请勿重复添加");
        }
        //
        var faEquipmentMeasures = new FaEquipmentMeasures();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createFaRepairEquipmentMeasuresDTO, faEquipmentMeasures);
        faEquipmentMeasures.setId(HandleUtils.UUID32());
        faEquipmentMeasures.setCreator(userDTO.getUsername());
        faEquipmentMeasures.setCreateDateTime(new Date());
        faEquipmentMeasures.setIsSoftDelete(false);
        int i = faEquipmentMeasuresMapper.insert(faEquipmentMeasures);
        return i == 1 ? RootResponse.success():RootResponse.error();
    }

    @Override
    public RootResponse updateFaRepairEquipmentMeasures(UpdateFaRepairEquipmentMeasuresDTO updateFaRepairEquipmentMeasuresDTO, LoginUserDTO userDTO) {
        // 没有name 字段，更新的时候，不做查重操作。
        var wrapper1 = new LambdaQueryWrapper<FaEquipmentMeasures>();
        wrapper1.eq(FaEquipmentMeasures::getName, updateFaRepairEquipmentMeasuresDTO.getName());
        wrapper1.eq(FaEquipmentMeasures::getProgrammeId, updateFaRepairEquipmentMeasuresDTO.getProgrammeId());
        wrapper1.eq(FaEquipmentMeasures::getIsSoftDelete, false);
        var faProgrammeMeasures1 = faEquipmentMeasuresMapper.selectOne(wrapper1);
        if (faProgrammeMeasures1!=null){
            if (!faProgrammeMeasures1.getId().equals(updateFaRepairEquipmentMeasuresDTO.getId())) {
//            var FaProgrammeMeasures2 = baseMapper.selectOne(wrapper1);
                return RootResponse.error("器材明细名称已存在，请勿重复添加");
            }
        }
        // the old data:
        var faProgrammeMeasures_old = faProgrammeMeasuresMapper.selectById(updateFaRepairEquipmentMeasuresDTO.getId());
        var faProgrammeMeasures=new FaProgrammeMeasures();
        BeanCopyUtils.copy(updateFaRepairEquipmentMeasuresDTO, faProgrammeMeasures);
        faProgrammeMeasures.setUpdater(userDTO.getUsername());
        faProgrammeMeasures.setUpdateDateTime(new Date());
        int i = faProgrammeMeasuresMapper.updateById(faProgrammeMeasures);
//        int i=1;
        return i == 1 ? RootResponse.success(faProgrammeMeasures_old) : RootResponse.error();
    }

    @Override
    public RootResponse deleteFaRepairEquipmentMeasures(String id, LoginUserDTO userDTO) {
        // 删除前，先判断是否在维修记录中存在。
        LambdaQueryWrapper<FaRepairRecord> faRepairRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        faRepairRecordLambdaQueryWrapper.eq(FaRepairRecord::getIsSoftDelete, false);
        faRepairRecordLambdaQueryWrapper.eq(FaRepairRecord::getRepairProgramId, faProgrammeMeasuresMapper.selectById(id).getProgrammeId());
        if (faRepairRecordMapper.selectList(faRepairRecordLambdaQueryWrapper).size()>0){
            return RootResponse.error("器材明细所在的维修方案可能在维修记录中，或者正被故障记录使用，请勿删除");
        }
        //
        var faEnsurePlan_old = faProgrammeMeasuresMapper.selectById(id);
        var wrapper = new LambdaQueryWrapper<FaProgrammeMeasures>();
        wrapper.eq(FaProgrammeMeasures::getId, id);
        wrapper.eq(FaProgrammeMeasures::getIsSoftDelete, false);
        var faProgrammeMeasures = faProgrammeMeasuresMapper.selectOne(wrapper);
        if (faProgrammeMeasures== null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        faProgrammeMeasures.setIsSoftDelete(true);
        faProgrammeMeasures.setUpdateDateTime(new Date());
        faProgrammeMeasures.setUpdater(userDTO.getUsername());
        faProgrammeMeasuresMapper.updateById(faProgrammeMeasures);
        return RootResponse.success(faEnsurePlan_old);
    }

    /**
     *
     * @param faRepairEquipmentPageDTO:
     * @return
     */
    @Override
    public RootResponse<PageResult<FaRepairEquipmentVO>> queryFaRepairEquipmentPage(FaRepairEquipmentPageDTO faRepairEquipmentPageDTO) {
        var wrapper = new LambdaQueryWrapper<FaRepairEquipment>();
        //
        wrapper.eq(FaRepairEquipment::getIsSoftDelete, false);
        // fuzzy query 模糊查询；
        if (faRepairEquipmentPageDTO.getName()!=null){
            // if not null, then get the specially query by the name;
            wrapper.like(FaRepairEquipment::getName,faRepairEquipmentPageDTO.getName());
        }
        //
        wrapper.orderByDesc(FaRepairEquipment::getCreateDateTime);
        // page helper
        PageHelper.startPage(faRepairEquipmentPageDTO.getPageNum(), faRepairEquipmentPageDTO.getPageSize());
        var taskLevels = faRepairEquipmentMapper.selectList(wrapper);
        var pageInfo = PageInfo.of(taskLevels);
        var pageList = pageInfo.getList();
        System.out.println(pageList);//
        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse createFaRepairEquipment(CreateFaRepairEquipmentDTO createFaRepairEquipmentDTO, LoginUserDTO userDTO) {
        //
        var wrapper = new LambdaQueryWrapper<FaRepairEquipment>();
        wrapper.eq(FaRepairEquipment::getName, createFaRepairEquipmentDTO.getName());
        wrapper.eq(FaRepairEquipment::getIsSoftDelete, false);
//        var FaRepairEquipment = FaRepairEquipmentMapper.selectOne(wrapper);
        if (faRepairEquipmentMapper.selectOne(wrapper) != null) {
            return RootResponse.error("维修器材已存在，请勿重复添加");
        }
        //
        var faRepairEquipment = new FaRepairEquipment();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createFaRepairEquipmentDTO, faRepairEquipment);
        faRepairEquipment.setId(HandleUtils.UUID32());
        faRepairEquipment.setCreator(userDTO.getUsername());
        faRepairEquipment.setCreateDateTime(new Date());
        faRepairEquipment.setIsSoftDelete(false);
        int i = faRepairEquipmentMapper.insert(faRepairEquipment);
        return i == 1 ? RootResponse.success():RootResponse.error();
    }

    @Override
    public RootResponse updateFaRepairEquipment(UpdateFaRepairEquipmentDTO updateFaRepairEquipmentDTO, LoginUserDTO userDTO) {
        var wrapper1 = new LambdaQueryWrapper<FaRepairEquipment>();
        wrapper1.eq(FaRepairEquipment::getName, updateFaRepairEquipmentDTO.getName());
        wrapper1.eq(FaRepairEquipment::getIsSoftDelete, false);
        var faRepairEquipment1=faRepairEquipmentMapper.selectOne(wrapper1);
        if (faRepairEquipment1!=null){// to solve the nullException here;
            if (!faRepairEquipment1.getId().equals(updateFaRepairEquipmentDTO.getId())) {
                return RootResponse.error("维修器材已存在，请勿重复添加");

            }
        }
        // the old data:
        var faRepairEquipment_old = faRepairEquipmentMapper.selectById(updateFaRepairEquipmentDTO.getId());
        var faRepairEquipment=new FaRepairEquipment();
        BeanCopyUtils.copy(updateFaRepairEquipmentDTO, faRepairEquipment);
        faRepairEquipment.setUpdater(userDTO.getUsername());
        faRepairEquipment.setUpdateDateTime(new Date());
        int i = faRepairEquipmentMapper.updateById(faRepairEquipment);
//        int i=1;
        return i == 1 ? RootResponse.success(faRepairEquipment_old) : RootResponse.error();
    }

    @Override
    public RootResponse deleteFaRepairEquipment(String id, LoginUserDTO userDTO) {
        // 判断是否在 方案明细中存在，如果存在则不能删除
        LambdaQueryWrapper<FaEquipmentMeasures> programmeMeasuresLambdaQueryWrapper = new LambdaQueryWrapper<>();
        programmeMeasuresLambdaQueryWrapper.eq(FaEquipmentMeasures::getIsSoftDelete,false);
        programmeMeasuresLambdaQueryWrapper.eq(FaEquipmentMeasures::getEquipmentId,id);
        //
        if (faEquipmentMeasuresMapper.selectList(programmeMeasuresLambdaQueryWrapper).size()>0){
            for (FaEquipmentMeasures programmeMeasures : faEquipmentMeasuresMapper.selectList(programmeMeasuresLambdaQueryWrapper)) {
                if (!faRepairProgrammeMapper.selectById(programmeMeasures.getProgrammeId()).getIsSoftDelete()) {
                    return RootResponse.error("该维修器材已在器材明细且存在于维修方案中，请勿删除！！！");
                }
            }
        }
        var faRepairEquipment_old = faRepairEquipmentMapper.selectById(id);
        var wrapper = new LambdaQueryWrapper<FaRepairEquipment>();
        wrapper.eq(FaRepairEquipment::getId, id);
        wrapper.eq(FaRepairEquipment::getIsSoftDelete, false);
        var faRepairEquipment = faRepairEquipmentMapper.selectOne(wrapper);
        if (faRepairEquipment== null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        faRepairEquipment.setIsSoftDelete(true);
        faRepairEquipment.setUpdateDateTime(new Date());
        faRepairEquipment.setUpdater(userDTO.getUsername());
        faRepairEquipmentMapper.updateById(faRepairEquipment);
        return RootResponse.success(faRepairEquipment_old);
    }

    /**
     *
     * @param faRepairDevicePageDTO:
     * @return :
     */
    @Override
    public RootResponse<PageResult<FaRepairDeviceVO>> queryFaRepairDevicePage(FaRepairDevicePageDTO faRepairDevicePageDTO) {
        var wrapper = new LambdaQueryWrapper<FaRepairDevice>();
        //
        wrapper.eq(FaRepairDevice::getIsSoftDelete, false);
        // fuzzy query 模糊查询；
        if (faRepairDevicePageDTO.getName()!=null){
            // if not null, then get the specially query by the name;
            wrapper.like(FaRepairDevice::getName,faRepairDevicePageDTO.getName());
        }
        //
        wrapper.orderByDesc(FaRepairDevice::getCreateDateTime);
        // page helper
        PageHelper.startPage(faRepairDevicePageDTO.getPageNum(), faRepairDevicePageDTO.getPageSize());
        var taskLevels = faRepairDeviceMapper.selectList(wrapper);
        var pageInfo = PageInfo.of(taskLevels);
        var pageList = pageInfo.getList();
        System.out.println(pageList);//
        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse createFaRepairDevice(CreateFaRepairDeviceDTO createFaRepairDeviceDTO, LoginUserDTO userDTO) {
        //
        var wrapper = new LambdaQueryWrapper<FaRepairDevice>();
        wrapper.eq(FaRepairDevice::getName, createFaRepairDeviceDTO.getName());
        wrapper.eq(FaRepairDevice::getIsSoftDelete, false);
//        var FaRepairDevice = FaRepairDeviceMapper.selectOne(wrapper);
        if (faRepairDeviceMapper.selectOne(wrapper) != null) {
            return RootResponse.error("维修设备已存在，请勿重复添加");
        }
        //
        var faRepairDevice = new FaRepairDevice();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createFaRepairDeviceDTO, faRepairDevice);
        faRepairDevice.setId(HandleUtils.UUID32());
        faRepairDevice.setCreator(userDTO.getUsername());
        faRepairDevice.setCreateDateTime(new Date());
        faRepairDevice.setIsSoftDelete(false);
        int i = faRepairDeviceMapper.insert(faRepairDevice);
        return i == 1 ? RootResponse.success():RootResponse.error();
    }

    @Override
    public RootResponse updateFaRepairDevice(UpdateFaRepairDeviceDTO updateFaRepairDeviceDTO, LoginUserDTO userDTO) {
        var wrapper1 = new LambdaQueryWrapper<FaRepairDevice>();
        wrapper1.eq(FaRepairDevice::getName, updateFaRepairDeviceDTO.getName());
        wrapper1.eq(FaRepairDevice::getIsSoftDelete, false);
        var faRepairDevice1=faRepairDeviceMapper.selectOne(wrapper1);
        if (faRepairDevice1!=null){// to solve the nullException here;
            if (!faRepairDevice1.getId().equals(updateFaRepairDeviceDTO.getId())) {
//            var FaRepairDevice2 = baseMapper.selectOne(wrapper1);
                return RootResponse.error( "维修设备已存在，请勿重复添加");

            }
        }
        // the old data:
        var faRepairDevice_old = faRepairDeviceMapper.selectById(updateFaRepairDeviceDTO.getId());
        var faRepairDevice=new FaRepairDevice();
        BeanCopyUtils.copy(updateFaRepairDeviceDTO, faRepairDevice);
        faRepairDevice.setUpdater(userDTO.getUsername());
        faRepairDevice.setUpdateDateTime(new Date());
        int i = faRepairDeviceMapper.updateById(faRepairDevice);
//        int i=1;
        return i == 1 ? RootResponse.success(faRepairDevice_old) : RootResponse.error();
    }

    @Override
    public RootResponse deleteFaRepairDevice(String id, LoginUserDTO userDTO) {
        // 判断是否在 方案明细中存在，如果存在则不能删除
        LambdaQueryWrapper<FaDeviceMeasures> programmeMeasuresLambdaQueryWrapper = new LambdaQueryWrapper<>();
        programmeMeasuresLambdaQueryWrapper.eq(FaDeviceMeasures::getIsSoftDelete,false);
        programmeMeasuresLambdaQueryWrapper.eq(FaDeviceMeasures::getDeviceId,id);
        // 判断
        if (faDeviceMeasuresMapper.selectList(programmeMeasuresLambdaQueryWrapper).size()>0){
            for (FaDeviceMeasures programmeMeasures : faDeviceMeasuresMapper.selectList(programmeMeasuresLambdaQueryWrapper)) {
                if (!faRepairProgrammeMapper.selectById(programmeMeasures.getProgrammeId()).getIsSoftDelete()) {
                    return RootResponse.error("该维修设备已在设备明细或正被维修方案使用中，请勿删除！！！");
                }
            }
        }
        var faRepairDevice_old = faRepairDeviceMapper.selectById(id);
        var wrapper = new LambdaQueryWrapper<FaRepairDevice>();
        wrapper.eq(FaRepairDevice::getId, id);
        wrapper.eq(FaRepairDevice::getIsSoftDelete, false);
        var faRepairDevice = faRepairDeviceMapper.selectOne(wrapper);
        if (faRepairDevice== null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        faRepairDevice.setIsSoftDelete(true);
        faRepairDevice.setUpdateDateTime(new Date());
        faRepairDevice.setUpdater(userDTO.getUsername());
        faRepairDeviceMapper.updateById(faRepairDevice);
        return RootResponse.success(faRepairDevice_old);
    }


    /**
     *
     * @param faRepairMeasuresPageDTO:
     * @return
     */
    @Override
    public RootResponse<PageResult<FaRepairMeasuresVO>> queryFaRepairMeasuresPage(FaRepairMeasuresPageDTO faRepairMeasuresPageDTO) {
        var wrapper = new LambdaQueryWrapper<FaRepairMeasures>();
        //
        wrapper.eq(FaRepairMeasures::getIsSoftDelete, false);
        // fuzzy query 模糊查询；
        if (faRepairMeasuresPageDTO.getName()!=null){
            // if not null, then get the specially query by the name;
            wrapper.like(FaRepairMeasures::getName,faRepairMeasuresPageDTO.getName());
        }
        //
        wrapper.orderByDesc(FaRepairMeasures::getCreateDateTime);
        // page helper
        PageHelper.startPage(faRepairMeasuresPageDTO.getPageNum(), faRepairMeasuresPageDTO.getPageSize());
        var taskLevels = faRepairMeasuresMapper.selectList(wrapper);
        var pageInfo = PageInfo.of(taskLevels);
        var pageList = pageInfo.getList();
        System.out.println(pageList);//
        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse createFaRepairMeasures(CreateFaRepairMeasuresDTO createFaRepairMeasuresDTO, LoginUserDTO userDTO) {
        //
        var wrapper = new LambdaQueryWrapper<FaRepairMeasures>();
        wrapper.eq(FaRepairMeasures::getName, createFaRepairMeasuresDTO.getName());
        wrapper.eq(FaRepairMeasures::getIsSoftDelete, false);
//        var FaRepairMeasures = FaRepairMeasuresMapper.selectOne(wrapper);
        if (faRepairMeasuresMapper.selectOne(wrapper) != null) {
            return RootResponse.error("维修措施已存在，请勿重复添加");
        }
        //
        var faRepairMeasures = new FaRepairMeasures();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createFaRepairMeasuresDTO, faRepairMeasures);
        faRepairMeasures.setId(HandleUtils.UUID32());
        faRepairMeasures.setCreator(userDTO.getUsername());
        faRepairMeasures.setCreateDateTime(new Date());
        faRepairMeasures.setIsSoftDelete(false);
        int i = faRepairMeasuresMapper.insert(faRepairMeasures);
        return i == 1 ? RootResponse.success():RootResponse.error();
    }

    @Override
    public RootResponse updateFaRepairMeasures(UpdateFaRepairMeasuresDTO updateFaRepairMeasuresDTO, LoginUserDTO userDTO) {
        var wrapper1 = new LambdaQueryWrapper<FaRepairMeasures>();
        // 查重
        if (wrapper1.eq(FaRepairMeasures::getName, updateFaRepairMeasuresDTO.getName())!=null){
            wrapper1.eq(FaRepairMeasures::getName, updateFaRepairMeasuresDTO.getName());
            wrapper1.eq(FaRepairMeasures::getIsSoftDelete, false);
            var faRepairMeasures=faRepairMeasuresMapper.selectOne(wrapper1);
            if (faRepairMeasures!=null){
                if (!faRepairMeasures.getId().equals(updateFaRepairMeasuresDTO.getId())) {
//            var FaRepairMeasures2 = baseMapper.selectOne(wrapper1);
                    return RootResponse.error("维修措施已存在，请勿重复添加");
                }
            }
        }
        // the old data:
        var faRepairMeasures_old = faRepairMeasuresMapper.selectById(updateFaRepairMeasuresDTO.getId());
        var faRepairMeasures=new FaRepairMeasures();
        BeanCopyUtils.copy(updateFaRepairMeasuresDTO, faRepairMeasures);
        faRepairMeasures.setUpdater(userDTO.getUsername());
        faRepairMeasures.setUpdateDateTime(new Date());
        int i = faRepairMeasuresMapper.updateById(faRepairMeasures);
//        int i=1;
        return i == 1 ? RootResponse.success(faRepairMeasures_old) : RootResponse.error();
    }

    @Override
    public RootResponse deleteFaRepairMeasures(String id, LoginUserDTO userDTO) {
        // 判断维修方案中是否有维修措施
//        LambdaQueryWrapper<FaProgrammeMeasures> programmeMeasuresLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<FaRepairProgramme> programmeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        programmeLambdaQueryWrapper.eq(FaRepairProgramme::getIsSoftDelete,false);
        programmeLambdaQueryWrapper.eq(FaRepairProgramme::getRepairMeasuresId,id);
        // 通过维修方案继续判断，如果存在维修方案在使用就不能删除
        if (faRepairProgrammeMapper.selectList(programmeLambdaQueryWrapper).size()>0){
//            for (FaProgrammeMeasures programmeMeasures : faProgrammeMeasuresMapper.selectList(programmeMeasuresLambdaQueryWrapper)) {
//                if (!faRepairProgrammeMapper.selectById(programmeMeasures.getProgrammeId()).getIsSoftDelete()) {
//                    return RootResponse.error("该维修措施已在方案明细中并在维修方案中使用，请勿删除！！！");
//                }
//            }
            return RootResponse.error("该维修措施已在维修方案中使用，请勿删除！！！");
        }

        var faRepairMeasures_old = faRepairMeasuresMapper.selectById(id);
        var wrapper = new LambdaQueryWrapper<FaRepairMeasures>();
        wrapper.eq(FaRepairMeasures::getId, id);
        wrapper.eq(FaRepairMeasures::getIsSoftDelete, false);
        var faRepairMeasures = faRepairMeasuresMapper.selectOne(wrapper);
        if (faRepairMeasures== null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        faRepairMeasures.setIsSoftDelete(true);
        faRepairMeasures.setUpdateDateTime(new Date());
        faRepairMeasures.setUpdater(userDTO.getUsername());
        faRepairMeasuresMapper.updateById(faRepairMeasures);
        return RootResponse.success(faRepairMeasures_old);
    }

    /**
     *
     * @param faRepairProgrammePageDTO:
     * @return
     */
    @Override
    public RootResponse<PageResult<FaRepairProgrammeVO>> queryFaRepairProgrammePage(FaRepairProgrammePageDTO faRepairProgrammePageDTO) {
//        var wrapper = new LambdaQueryWrapper<FaRepairProgramme>();
//        //
//        wrapper.eq(FaRepairProgramme::getIsSoftDelete, false);
//        // fuzzy query 模糊查询；
//        if (faRepairProgrammePageDTO.getName()!=null){
//            // if not null, then get the specially query by the name;
//            wrapper.like(FaRepairProgramme::getName,faRepairProgrammePageDTO.getName());
//        }
//        //
//        wrapper.orderByDesc(FaRepairProgramme::getCreateDateTime);
        // page helper
        PageHelper.startPage(faRepairProgrammePageDTO.getPageNum(), faRepairProgrammePageDTO.getPageSize());

        var faRepairProgrammes = faRepairProgrammeMapper.selectFaRepairProgramme(faRepairProgrammePageDTO);

        var pageInfo = PageInfo.of(faRepairProgrammes);
        var pageList = pageInfo.getList();
        // add the info of the device and equipment;
        pageList.forEach(elem->{
            LambdaQueryWrapper<FaDeviceMeasures> deviceWra = new LambdaQueryWrapper<>();
            deviceWra.eq(FaDeviceMeasures::getIsSoftDelete, false);
            deviceWra.eq(FaDeviceMeasures::getProgrammeId, elem.getId());
            List<FaDeviceMeasures> faDeviceMeasures = faDeviceMeasuresMapper.selectList(deviceWra);
            List<Map<String,String>> devicelst = new ArrayList<>();
            if (faDeviceMeasures.size()>0){
                for (FaDeviceMeasures deviceMeasure : faDeviceMeasures) {
                    Map<String, String> device = new HashMap<>();
                    device.put("id",deviceMeasure.getDeviceId());
                    String name = faRepairDeviceMapper.selectById(deviceMeasure.getDeviceId()).getName();
                    device.put("name",name);
                    devicelst.add(device);
                }
            }
            // equipment
            LambdaQueryWrapper<FaEquipmentMeasures> equipmentWra = new LambdaQueryWrapper<>();
            equipmentWra.eq(FaEquipmentMeasures::getIsSoftDelete, false);
            equipmentWra.eq(FaEquipmentMeasures::getProgrammeId, elem.getId());
            List<FaEquipmentMeasures> equipmentMeasures = faEquipmentMeasuresMapper.selectList(equipmentWra);
            List<Map<String,String>> equipmentlst = new ArrayList<>();
            if (equipmentMeasures.size()>0){
                for (FaEquipmentMeasures equipmentMeasures1 : equipmentMeasures) {
                    Map<String, String> equipment = new HashMap<>();
                    equipment.put("id",equipmentMeasures1.getEquipmentId());
                    String name = faRepairEquipmentMapper.selectById(equipmentMeasures1.getEquipmentId()).getName();
                    equipment.put("name",name);
                    equipmentlst.add(equipment);
                }
            }
           //
            elem.setDeviceList(devicelst);
            elem.setEquipmentList(equipmentlst);
        });
//        pageList.add(faRepairEquipmentMapper.selectById(fa));
        System.out.println(pageList);//
        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse createFaRepairProgramme(CreateFaRepairProgrammeDTO createFaRepairProgrammeDTO, LoginUserDTO userDTO) {
        //
        var wrapper = new LambdaQueryWrapper<FaRepairProgramme>();
        System.out.println(createFaRepairProgrammeDTO);
        wrapper.eq(FaRepairProgramme::getName, createFaRepairProgrammeDTO.getName());
        wrapper.eq(FaRepairProgramme::getIsSoftDelete, false);
//        var FaRepairProgramme = FaRepairProgrammeMapper.selectOne(wrapper);
        if (faRepairProgrammeMapper.selectOne(wrapper) != null) {
            return RootResponse.error( "维修方案已存在，请勿重复添加");
        }
        //
        var faRepairProgramme = new FaRepairProgramme();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createFaRepairProgrammeDTO, faRepairProgramme);
        faRepairProgramme.setId(HandleUtils.UUID32());
        faRepairProgramme.setCreator(userDTO.getUsername());
        faRepairProgramme.setCreateDateTime(new Date());
        faRepairProgramme.setIsSoftDelete(false);
        int i = faRepairProgrammeMapper.insert(faRepairProgramme);
        // create to device and equipment:
        List<String> deviceIds = createFaRepairProgrammeDTO.getDeviceIds();
        List<String> equipmentIds = createFaRepairProgrammeDTO.getEquipmentIds();
        if (deviceIds.size()>0){
            for (String deviceId : deviceIds) {
                FaDeviceMeasures deviceMeasures = new FaDeviceMeasures();
                deviceMeasures.setId(HandleUtils.UUID32());
                deviceMeasures.setProgrammeId(faRepairProgramme.getId());
                deviceMeasures.setDeviceId(deviceId);
                deviceMeasures.setCreator(userDTO.getUsername());
                deviceMeasures.setCreateDateTime(new Date());
                deviceMeasures.setIsSoftDelete(false);
                //
                faDeviceMeasuresMapper.insert(deviceMeasures);
            }
        }
        if (equipmentIds.size()>0){
            for (String equipmentId : equipmentIds) {
                FaEquipmentMeasures euipmentMeasures = new FaEquipmentMeasures();
                euipmentMeasures.setId(HandleUtils.UUID32());
                euipmentMeasures.setProgrammeId(faRepairProgramme.getId());
                euipmentMeasures.setEquipmentId(equipmentId);
                euipmentMeasures.setCreator(userDTO.getUsername());
                euipmentMeasures.setCreateDateTime(new Date());
                euipmentMeasures.setIsSoftDelete(false);
                //
                faEquipmentMeasuresMapper.insert(euipmentMeasures);
            }
        }
        return i == 1 ? RootResponse.success():RootResponse.error();
    }

    @Override
    public RootResponse updateFaRepairProgramme(UpdateFaRepairProgrammeDTO updateFaRepairProgrammeDTO, LoginUserDTO userDTO) {
        var wrapper1 = new LambdaQueryWrapper<FaRepairProgramme>();
        wrapper1.eq(FaRepairProgramme::getName, updateFaRepairProgrammeDTO.getName());
        wrapper1.eq(FaRepairProgramme::getIsSoftDelete, false);
       var faRepairProgramme1= faRepairProgrammeMapper.selectOne(wrapper1);
        if(faRepairProgramme1!=null){//
            if (!faRepairProgramme1.getId().equals(updateFaRepairProgrammeDTO.getId())) {
//            var FaRepairProgramme2 = baseMapper.selectOne(wrapper1);
                return RootResponse.error("维修方案已存在，请勿重复添加");

            }
        }
        // the old data:
        var faRepairProgramme_old = faRepairProgrammeMapper.selectById(updateFaRepairProgrammeDTO.getId());
        var faRepairProgramme=new FaRepairProgramme();
        BeanCopyUtils.copy(updateFaRepairProgrammeDTO, faRepairProgramme);
        faRepairProgramme.setUpdater(userDTO.getUsername());
        faRepairProgramme.setUpdateDateTime(new Date());
        int i = faRepairProgrammeMapper.updateById(faRepairProgramme);
//        int i=1;
        // update to device and equipment:
        List<String> deviceIds = updateFaRepairProgrammeDTO.getDeviceIds();
        List<String> equipmentIds = updateFaRepairProgrammeDTO.getEquipmentIds();
        // delete,clear:
        if(deviceIds.size()>0){
            LambdaQueryWrapper<FaDeviceMeasures> deviceMeasuresLambdaQueryWrapper = new LambdaQueryWrapper<>();
            deviceMeasuresLambdaQueryWrapper.eq(FaDeviceMeasures::getIsSoftDelete, false);
            deviceMeasuresLambdaQueryWrapper.eq(FaDeviceMeasures::getProgrammeId, updateFaRepairProgrammeDTO.getId());
            List<FaDeviceMeasures> faDeviceMeasures = faDeviceMeasuresMapper.selectList(deviceMeasuresLambdaQueryWrapper);
            if (faDeviceMeasures.size()>0){
                for (FaDeviceMeasures faDeviceMeasure : faDeviceMeasures) {
                    faDeviceMeasuresMapper.deleteById(faDeviceMeasure.getId());
                }
            }
            for (String deviceId : deviceIds) {
                FaDeviceMeasures deviceMeasures = new FaDeviceMeasures();
                deviceMeasures.setId(HandleUtils.UUID32());
                deviceMeasures.setProgrammeId(faRepairProgramme.getId());
                deviceMeasures.setDeviceId(deviceId);
                deviceMeasures.setCreator(userDTO.getUsername());
                deviceMeasures.setCreateDateTime(new Date());
                deviceMeasures.setIsSoftDelete(false);
                //
                faDeviceMeasuresMapper.insert(deviceMeasures);
            }
        }
        // equipment
        if(equipmentIds.size()>0){
            LambdaQueryWrapper<FaEquipmentMeasures> deviceMeasuresLambdaQueryWrapper = new LambdaQueryWrapper<>();
            deviceMeasuresLambdaQueryWrapper.eq(FaEquipmentMeasures::getIsSoftDelete, false);
            deviceMeasuresLambdaQueryWrapper.eq(FaEquipmentMeasures::getProgrammeId, updateFaRepairProgrammeDTO.getId());
            List<FaEquipmentMeasures> faDeviceMeasures = faEquipmentMeasuresMapper.selectList(deviceMeasuresLambdaQueryWrapper);
            if (faDeviceMeasures.size()>0){//
                for (FaEquipmentMeasures faDeviceMeasure : faDeviceMeasures) {
                    faEquipmentMeasuresMapper.deleteById(faDeviceMeasure.getId());
                }
            }
            for (String equipmentId : equipmentIds) {
                FaEquipmentMeasures euipmentMeasures = new FaEquipmentMeasures();
                euipmentMeasures.setId(HandleUtils.UUID32());
                euipmentMeasures.setProgrammeId(faRepairProgramme.getId());
                euipmentMeasures.setEquipmentId(equipmentId);
                euipmentMeasures.setCreator(userDTO.getUsername());
                euipmentMeasures.setCreateDateTime(new Date());
                euipmentMeasures.setIsSoftDelete(false);
                //
                faEquipmentMeasuresMapper.insert(euipmentMeasures);
            }
        }

        return i == 1 ? RootResponse.success(faRepairProgramme_old) : RootResponse.error();
    }

    @Override
    public RootResponse deleteFaRepairProgramme(String id, LoginUserDTO userDTO) {
        var faRepairProgramme_old = faRepairProgrammeMapper.selectById(id);
        var wrapper = new LambdaQueryWrapper<FaRepairProgramme>();
        // 删除前，先判断是否在维修记录中存在。
        LambdaQueryWrapper<FaRepairRecord> faRepairRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        faRepairRecordLambdaQueryWrapper.eq(FaRepairRecord::getIsSoftDelete, false);
        faRepairRecordLambdaQueryWrapper.eq(FaRepairRecord::getRepairProgramId, id);
        if (faRepairRecordMapper.selectList(faRepairRecordLambdaQueryWrapper).size()>0){
            return RootResponse.error("维修方案可能在维修记录中，或者正被故障记录使用，请勿删除");
        }
        wrapper.eq(FaRepairProgramme::getId, id);
        wrapper.eq(FaRepairProgramme::getIsSoftDelete, false);
        var faRepairProgramme = faRepairProgrammeMapper.selectOne(wrapper);
        if (faRepairProgramme== null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        faRepairProgramme.setIsSoftDelete(true);
        faRepairProgramme.setUpdateDateTime(new Date());
        faRepairProgramme.setUpdater(userDTO.getUsername());
        faRepairProgrammeMapper.updateById(faRepairProgramme);
        return RootResponse.success(faRepairProgramme_old);
    }

    /**
     *
     * @param faRepairRecordPageDTO:
     * @return
     */
    @Override
    public RootResponse<PageResult<FaRepairRecordVO>> queryFaRepairRecordPage(FaRepairRecordPageDTO faRepairRecordPageDTO) {
        // page helper
        PageHelper.startPage(faRepairRecordPageDTO.getPageNum(), faRepairRecordPageDTO.getPageSize());
        var faRepairRecords = faRepairRecordMapper.selectFaRepairRecord(faRepairRecordPageDTO);
        var pageInfo = PageInfo.of(faRepairRecords);
        var pageList = pageInfo.getList();
        System.out.println(pageList);//
        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse createFaRepairRecord(CreateFaRepairRecordDTO createFaRepairRecordDTO, LoginUserDTO userDTO) {

        var faRepairRecord = new FaRepairRecord();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createFaRepairRecordDTO, faRepairRecord);
        faRepairRecord.setId(HandleUtils.UUID32());
        faRepairRecord.setCreator(userDTO.getUsername());
        faRepairRecord.setCreateDateTime(new Date());
        faRepairRecord.setIsSoftDelete(false);
        faRepairRecord.setRepairUser(userDTO.getUsername());
//        // set time;
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
//        String str = createFaRepairRecordDTO.getRepairTime();
//        Date date = null;
//        try {
//            date = dateFormat.parse(str);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        faRepairRecord.setRepairTime(createFaRepairRecordDTO.getRepairTime());
        int i = faRepairRecordMapper.insert(faRepairRecord);
        FaFaultRecord faFaultRecord = faFaultRecordMapper.selectById(faRepairRecord.getFaultRecordId());
        if (faFaultRecord.getIsDeal()!=1){
            faFaultRecord.setIsDeal(1);
            faFaultRecord.setUpdater(userDTO.getUsername());
            faFaultRecord.setUpdateDateTime(new Date());
            faFaultRecordMapper.updateById(faFaultRecord);
        }
        var sysMessage= new SysMessage();
        sysMessage.setId(HandleUtils.UUID32());
        sysMessage.setCreateDateTime(new Date());
        sysMessage.setCreator("手动维修");
        sysMessage.setContent(faFaultRecord.getRemarks());
        sysMessage.setType(0);
        sysMessage.setSource("手动维修");
        sysMessage.setIsSoftDelete(false);
        sysMessageMapper.insert(sysMessage);

        return i == 1 ? RootResponse.success():RootResponse.error();
    }

    @Override
    public RootResponse updateFaRepairRecord(UpdateFaRepairRecordDTO updateFaRepairRecordDTO, LoginUserDTO userDTO) {
        var wrapper1 = new LambdaQueryWrapper<FaRepairRecord>();
        wrapper1.eq(FaRepairRecord::getId, updateFaRepairRecordDTO.getId());
        wrapper1.eq(FaRepairRecord::getIsSoftDelete, false);
        var faRepairRecord1=faRepairRecordMapper.selectOne(wrapper1);
        if (faRepairRecord1!=null&&!faRepairRecord1.getId().equals(updateFaRepairRecordDTO.getId())) {
//            var FaRepairRecord2 = baseMapper.selectOne(wrapper1);
            return RootResponse.error("维修记录已存在，请勿重复添加");

        }
        // the old data:
        var faRepairRecord_old = faRepairRecordMapper.selectById(updateFaRepairRecordDTO.getId());
        var faRepairRecord=new FaRepairRecord();
        BeanCopyUtils.copy(updateFaRepairRecordDTO, faRepairRecord);
        faRepairRecord.setUpdater(userDTO.getUsername());
        faRepairRecord.setUpdateDateTime(new Date());
//        // set time;
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        String str = updateFaRepairRecordDTO.getRepairTime();
//        Date date = null;
//        try {
//            date = dateFormat.parse(str);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        faRepairRecord.setRepairTime(updateFaRepairRecordDTO.getRepairTime());
        int i = faRepairRecordMapper.updateById(faRepairRecord);
        // isDeal?
        FaFaultRecord faFaultRecord = faFaultRecordMapper.selectById(faRepairRecord.getFaultRecordId());
        if (faFaultRecord.getIsDeal()!=1){
            faFaultRecord.setIsDeal(1);
            faFaultRecord.setUpdater(userDTO.getUsername());
            faFaultRecord.setUpdateDateTime(new Date());
            faFaultRecordMapper.updateById(faFaultRecord);
        }
//        int i=1;
        return i == 1 ? RootResponse.success(faRepairRecord_old) : RootResponse.error();
    }

    @Override
    public RootResponse deleteFaRepairRecord(String id, LoginUserDTO userDTO) {
        var faRepairRecord_old = faRepairRecordMapper.selectById(id);
        var wrapper = new LambdaQueryWrapper<FaRepairRecord>();
        wrapper.eq(FaRepairRecord::getId, id);
        wrapper.eq(FaRepairRecord::getIsSoftDelete, false);
        var faRepairRecord = faRepairRecordMapper.selectOne(wrapper);
        if (faRepairRecord== null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        faRepairRecord.setIsSoftDelete(true);
        faRepairRecord.setUpdateDateTime(new Date());
        faRepairRecord.setUpdater(userDTO.getUsername());
        faRepairRecordMapper.updateById(faRepairRecord);
        return RootResponse.success(faRepairRecord_old);
    }

    /**
     *
     * @param faTechnicalDocumentationPageDTO
     * @return
     */
    @Override
    public RootResponse<PageResult<FaTechnicalDocumentationVO>> queryFaTechnicalDocumentationPage(FaTechnicalDocumentationPageDTO faTechnicalDocumentationPageDTO) {
        var wrapper = new LambdaQueryWrapper<FaTechnicalDocumentation>();
        //
        wrapper.eq(FaTechnicalDocumentation::getIsSoftDelete, false);
        // fuzzy query 模糊查询；
        if (faTechnicalDocumentationPageDTO.getName()!=null){
            // if not null, then get the specially query by the name;
            wrapper.like(FaTechnicalDocumentation::getName,faTechnicalDocumentationPageDTO.getName());
        }
        //
        wrapper.orderByDesc(FaTechnicalDocumentation::getCreateDateTime);
        // page helper
        PageHelper.startPage(faTechnicalDocumentationPageDTO.getPageNum(), faTechnicalDocumentationPageDTO.getPageSize());

        var faTechnicalDocumentations = faTechnicalDocumentationMapper.selectList(wrapper);
        var pageInfo = PageInfo.of(faTechnicalDocumentations);
        var pageList = pageInfo.getList();
        System.out.println(pageList);//
        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse createFaTechnicalDocumentation(CreateFaTechnicalDocumentationDTO createFaTechnicalDocumentationDTO, LoginUserDTO userDTO) {
        //
        var wrapper = new LambdaQueryWrapper<FaTechnicalDocumentation>();
        wrapper.eq(FaTechnicalDocumentation::getName, createFaTechnicalDocumentationDTO.getName());
        wrapper.eq(FaTechnicalDocumentation::getIsSoftDelete, false);
//        var FaTechnicalDocumentation = FaTechnicalDocumentationMapper.selectOne(wrapper);
        if (faTechnicalDocumentationMapper.selectOne(wrapper) != null) {
            return RootResponse.error("工卡已存在，请勿重复添加");
        }
        //
        var faTechnicalDocumentation = new FaTechnicalDocumentation();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createFaTechnicalDocumentationDTO, faTechnicalDocumentation);
        faTechnicalDocumentation.setId(HandleUtils.UUID32());
        faTechnicalDocumentation.setCreator(userDTO.getUsername());
        faTechnicalDocumentation.setCreateDateTime(new Date());
        faTechnicalDocumentation.setIsSoftDelete(false);
        int i = faTechnicalDocumentationMapper.insert(faTechnicalDocumentation);
        return i == 1 ? RootResponse.success():RootResponse.error();
    }

    @Override
    public RootResponse updateFaTechnicalDocumentation(UpdateFaTechnicalDocumentationDTO updateFaTechnicalDocumentationDTO, LoginUserDTO userDTO) {
        var wrapper1 = new LambdaQueryWrapper<FaTechnicalDocumentation>();
        wrapper1.eq(FaTechnicalDocumentation::getName, updateFaTechnicalDocumentationDTO.getName());
        wrapper1.eq(FaTechnicalDocumentation::getIsSoftDelete, false);

        if (faTechnicalDocumentationMapper.selectOne(wrapper1)!=null){
            if (!faTechnicalDocumentationMapper.selectOne(wrapper1).getId().equals(updateFaTechnicalDocumentationDTO.getId())) {
//            var FaTechnicalDocumentation2 = baseMapper.selectOne(wrapper1);
                return RootResponse.error("工卡已存在，请勿重复添加");

            }
        }
        // the old data:
        var faTechnicalDocumentation_old = faTechnicalDocumentationMapper.selectById(updateFaTechnicalDocumentationDTO.getId());
        var faTechnicalDocumentation=new FaTechnicalDocumentation();
        BeanCopyUtils.copy(updateFaTechnicalDocumentationDTO, faTechnicalDocumentation);
        faTechnicalDocumentation.setUpdater(userDTO.getUsername());
        faTechnicalDocumentation.setUpdateDateTime(new Date());
        int i = faTechnicalDocumentationMapper.updateById(faTechnicalDocumentation);
//        int i=1;
        return i == 1 ? RootResponse.success(faTechnicalDocumentation_old) : RootResponse.error();
    }

    @Override
    public RootResponse deleteFaTechnicalDocumentation(String id, LoginUserDTO userDTO) {
        // 判断是否在 维修方案中存在，如果存在则不能删除
        LambdaQueryWrapper<FaRepairProgramme> faRepairProgrammeQueryWrapper = new LambdaQueryWrapper<>();
        faRepairProgrammeQueryWrapper.eq(FaRepairProgramme::getIsSoftDelete,false);
        faRepairProgrammeQueryWrapper.eq(FaRepairProgramme::getDocumentId,id);
        if (faRepairProgrammeMapper.selectList(faRepairProgrammeQueryWrapper).size()>0){
            return RootResponse.error("该工卡已在维修方案中，请勿删除!!!");
        }
        LambdaQueryWrapper<FaEnsure> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(FaEnsure::getDocumentId, id);
        wrapper1.eq(FaEnsure::getIsSoftDelete, false);
        if (faEnsureMapper.selectList(wrapper1).size()>0){
            return RootResponse.error("该工卡已被保障基础信息绑定，请勿删除!!!");
        }
        var faTechnicalDocumentation_old = faTechnicalDocumentationMapper.selectById(id);
        var wrapper = new LambdaQueryWrapper<FaTechnicalDocumentation>();
        wrapper.eq(FaTechnicalDocumentation::getId, id);
        wrapper.eq(FaTechnicalDocumentation::getIsSoftDelete, false);
        var faTechnicalDocumentation = faTechnicalDocumentationMapper.selectOne(wrapper);
        if (faTechnicalDocumentation== null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        faTechnicalDocumentation.setIsSoftDelete(true);
        faTechnicalDocumentation.setUpdateDateTime(new Date());
        faTechnicalDocumentation.setUpdater(userDTO.getUsername());
        faTechnicalDocumentationMapper.updateById(faTechnicalDocumentation);
        return RootResponse.success(faTechnicalDocumentation_old);
    }

    /**
     *
     * @param faFaultRecordPageDTO
     * @return
     */

    @Override
    public RootResponse<PageResult<FaFaultRecordVO>> queryFaFaultRecordPage(FaFaultRecordPageDTO faFaultRecordPageDTO, LoginUserDTO userDTO) {
        PageHelper.startPage(faFaultRecordPageDTO.getPageNum(), faFaultRecordPageDTO.getPageSize());
        // date format:
//        // set time;
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        String str = faFaultRecordPageDTO.getDownTimeStart();
//        Date date = null;
//        try {
//            date = dateFormat.parse(str);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        faFaultRecordPageDTO.set(date);

        // 添加区域：
        faFaultRecordPageDTO.setAreaId(userDTO.getAreaId());
        //
        var faFaultRecords = faFaultRecordMapper.selectFaultRecordAllInfo(faFaultRecordPageDTO);
        var pageInfo = PageInfo.of(faFaultRecords);
        var pageList = pageInfo.getList();
        // get the structure id:
//        for (FaFaultRecord record : pageList) {
//            record.getStructureId();
//        }
//        System.out.println(pageList);//
        pageList.forEach(temp->{
            // radar type :
            String radarTypeId = "";
            if (temp.getStructureId()!=null){
                var raTree=raTreeMapper.selectById(temp.getStructureId());
                if (raTree!=null&&raTree.getRadarId()!=null){
                    var raRadar=raRadarMapper.selectById(raTree.getRadarId());
                    if(raRadar!=null){
                        radarTypeId =raRadar.getTypeId();
                    }else {
                        return;
                    }

                }else {
                    return;
                }
            }else {
                return;
            }

            temp.setRadarTypeId(radarTypeId);
            if (fiRadarTypeMapper.selectById(radarTypeId)!=null){
                temp.setRadarTypeName(fiRadarTypeMapper.selectById(radarTypeId).getName());
            }
            //
            String ids = "";
            String names = "";
            var phmFaultRecord = new FaFaultRecordVO();
            BeanCopyUtils.copy(temp,phmFaultRecord);
            if(phmFaultRecord.getStructureId()!=null){
                if(raTreeMapper.selectById(phmFaultRecord.getStructureId())!=null) {
                    if(raTreeMapper.selectById(phmFaultRecord.getStructureId()).getRadarId()!=null) {
                        ids += raTreeMapper.selectById(phmFaultRecord.getStructureId()).getRadarId();
                        if(raTreeMapper.selectById(phmFaultRecord.getStructureId()).getSystemId()!=null){
                            ids += "-" + raTreeMapper.selectById(phmFaultRecord.getStructureId()).getSystemId();
                        }
                        if(raTreeMapper.selectById(phmFaultRecord.getStructureId()).getExtensionId()!=null) {
                            ids += "-" + raTreeMapper.selectById(phmFaultRecord.getStructureId()).getExtensionId();
                        }
                        if(raTreeMapper.selectById(phmFaultRecord.getStructureId()).getKeyPartsModelId()!=null) {
                            ids += "-" + raTreeMapper.selectById(phmFaultRecord.getStructureId()).getKeyPartsModelId();
                        }
                    }
                }
            }// get ids;
            if(phmFaultRecord.getStructureId()!=null&&raTreeMapper.selectById(phmFaultRecord.getStructureId())!=null){
                if(raTreeMapper.selectById(phmFaultRecord.getStructureId()).getRadarId()!=null
                &&raRadarMapper.selectById(raTreeMapper.selectById(phmFaultRecord.getStructureId()).getRadarId())!=null
                ){
                    names+=raRadarMapper.selectById(
                            raTreeMapper.selectById(phmFaultRecord.getStructureId()).getRadarId()
                    ).getName();
                }
                if(raTreeMapper.selectById(phmFaultRecord.getStructureId()).getSystemId()!=null
                &&raSystemMapper.selectById(raTreeMapper.selectById(phmFaultRecord.getStructureId()).getSystemId())!=null
                ){
                    names+="-"+raSystemMapper.selectById(
                            raTreeMapper.selectById(phmFaultRecord.getStructureId()).getSystemId()
                    ).getName();
                }
                if(raTreeMapper.selectById(phmFaultRecord.getStructureId()).getExtensionId()!=null
                &&raExtensionMapper.selectById(raTreeMapper.selectById(phmFaultRecord.getStructureId()).getExtensionId())!=null
                ){
                    names+="-"+raExtensionMapper.selectById(
                            raTreeMapper.selectById(phmFaultRecord.getStructureId()).getExtensionId()
                    ).getName();
                }
                if(raTreeMapper.selectById(phmFaultRecord.getStructureId()).getKeyPartsModelId()!=null
                &&raKeyPartsModelMapper.selectById(raTreeMapper.selectById(phmFaultRecord.getStructureId()).getKeyPartsModelId())!=null
                ){
                    names+="-"+raKeyPartsModelMapper.selectById(
                            raTreeMapper.selectById(phmFaultRecord.getStructureId()).getKeyPartsModelId()
                    ).getName();
                }
            }// get names;
            temp.setRaEquipmentIds(ids);
            temp.setRaEquipmentNames(names);
//            newList.add(phmFaultRecord);
                }
        );
        // 如果查询条件包括故障设备，则进行故障设备的模糊筛选。
//        if(!StringUtils.isEmpty(faFaultRecordPageDTO.getRepairEquipmentName())){
//            // 迭代器进行判断删除：
//            Iterator<FaFaultRecordVO> faFaultRecordVOIterator = pageList.iterator();
//            while(faFaultRecordVOIterator.hasNext()){
//                FaFaultRecordVO faFaultRecordVO = faFaultRecordVOIterator.next();
//                if (!faFaultRecordVO.getRaEquipmentNames().contains(faFaultRecordPageDTO.getRepairEquipmentName())){
//                    faFaultRecordVOIterator.remove();
//                }
//            }
//        }

        return RootResponse.success(PageResult.page(pageList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse<FaultRecordPhenomenonVO> queryFaFaultRecordPhenomenonIds(String faultRecordId) {
        var lst_idsRemarks = faPhenomenonRecordMapper.selectPhenomenonByFaultRecordId(faultRecordId);
        var fprVO = new FaultRecordPhenomenonVO();
//        fprVO.setPhmRecordId(faultRecordId);
        var phenomenonIdsAndRemarks = new ArrayList<Map<String,String>>();
        lst_idsRemarks.forEach(elem-> {
            Map<String,String> temp = new HashMap<>();
            temp.put("id",elem.getId());
            temp.put("remark",elem.getRemarks());
            temp.put("specialId",elem.getPhenomenonSpecialId());
            phenomenonIdsAndRemarks.add(temp);
        });
        fprVO.setPhenomenonIdsAndRemarks(phenomenonIdsAndRemarks);// not need;

        return RootResponse.success(phenomenonIdsAndRemarks);
    }

    @Override
    public RootResponse createFaultPhenomenonRecord(FaultRecordPhenomenonDTO faultRecordPhenomenonDTO, LoginUserDTO userDTO) {
        var queryWrapper = new LambdaQueryWrapper<FaPhenomenonRecord>();
        queryWrapper.eq(FaPhenomenonRecord::getFaultRecordId, faultRecordPhenomenonDTO.getPhmRecordId());
        queryWrapper.eq(FaPhenomenonRecord::getIsSoftDelete, false);
        var role = faPhenomenonRecordMapper.selectList(queryWrapper);
        if (role == null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        // delete the relations:
        var wrapper = new LambdaQueryWrapper<FaPhenomenonRecord>();
        wrapper.eq(FaPhenomenonRecord::getFaultRecordId, faultRecordPhenomenonDTO.getPhmRecordId());
        faPhenomenonRecordMapper.delete(wrapper);
        // 添加关联关系
        if (faultRecordPhenomenonDTO.getPhenomenonIds() != null && !faultRecordPhenomenonDTO.getPhenomenonIds().isEmpty()) {
            faultRecordPhenomenonDTO.getPhenomenonIds().forEach(m -> {
                var faPhenomenonRecord= new FaPhenomenonRecord();
                faPhenomenonRecord.setId(HandleUtils.UUID32());
                faPhenomenonRecord.setCreator(userDTO.getUsername());
                faPhenomenonRecord.setCreateDateTime(new Date());
                faPhenomenonRecord.setPhenomenonId(m);
                faPhenomenonRecord.setIsSoftDelete(false);
                faPhenomenonRecord.setFaultRecordId(faultRecordPhenomenonDTO.getPhmRecordId());
                faPhenomenonRecord.setRemarks("添加成功");
                int j=faPhenomenonRecordMapper.insert(faPhenomenonRecord);
                if (j != 1) {
                    throw new RuntimeException("失败");
                }
            });
        }

        return RootResponse.success(role);
    }

    @Override
    public RootResponse createFaFaultRecord(CreateFaFaultRecordDTO createFaFaultRecordDTO, LoginUserDTO userDTO) {
        //
//        var wrapper = new LambdaQueryWrapper<FaFaultRecord>();
////        wrapper.eq(FaFaultRecord::getName, createFaFaultRecordDTO.getName());
////        wrapper.eq(FaFaultRecord::getIsSoftDelete, false);
//////        var FaFaultRecord = FaFaultRecordMapper.selectOne(wrapper);
////        if (faFaultRecordMapper.selectOne(wrapper) != null) {
////            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(), "工卡已存在，请勿重复添加");
////        }
        //

        var faFaultRecord = new FaFaultRecord();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createFaFaultRecordDTO, faFaultRecord);
        faFaultRecord.setId(HandleUtils.UUID32());
        faFaultRecord.setCreator(userDTO.getUsername());
        faFaultRecord.setCreateDateTime(new Date());
        faFaultRecord.setIsSoftDelete(false);
        // set time;
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        String str = createFaFaultRecordDTO.getDownTime();
//        Date date = null;
//        try {
//            date = dateFormat.parse(str);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        faFaultRecord.setDownTime(createFaFaultRecordDTO.getDownTime());
        int i = faFaultRecordMapper.insert(faFaultRecord);
        // is_deal ?
        if (faFaultRecord.getIsDeal()==1) {
            var faRepairRecord = new FaRepairRecord();
            faRepairRecord.setId(HandleUtils.UUID32());
            faRepairRecord.setCreator(userDTO.getUsername());
            faRepairRecord.setCreateDateTime(new Date());
            faRepairRecord.setIsSoftDelete(false);
            faRepairRecord.setRepairTime(faFaultRecord.getDownTime());// 故障时间
            faRepairRecord.setRepairUser(userDTO.getUsername());// user id
            faRepairRecord.setFaultRecordId(faFaultRecord.getId());// PHM_Record id;
            faRepairRecord.setRemarks(userDTO.getUsername()+"维修");
            //
            faRepairRecordMapper.insert(faRepairRecord);// debug here, the id is null;
        }
        return i == 1 ? RootResponse.success(faFaultRecord.getId()):RootResponse.error();
    }

    @Override
    public RootResponse updateFaFaultRecord(UpdateFaFaultRecordDTO updateFaFaultRecordDTO, LoginUserDTO userDTO) {
//        var wrapper1 = new LambdaQueryWrapper<FaFaultRecord>();
//        wrapper1.eq(FaFaultRecord::getName, updateFaFaultRecordDTO.getName());
//        wrapper1.eq(FaFaultRecord::getIsSoftDelete, false);
//
//        if (faFaultRecordMapper.selectOne(wrapper1)!=null){
//            if (!faFaultRecordMapper.selectOne(wrapper1).getId().equals(updateFaFaultRecordDTO.getId())) {
////            var FaFaultRecord2 = baseMapper.selectOne(wrapper1);
//                return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(), "工卡已存在，请勿重复添加");
//
//            }
//        }
        // the old data:
        var faFaultRecord_old = faFaultRecordMapper.selectById(updateFaFaultRecordDTO.getId());
        var faFaultRecord=new FaFaultRecord();
        BeanCopyUtils.copy(updateFaFaultRecordDTO, faFaultRecord);
        faFaultRecord.setUpdater(userDTO.getUsername());
        faFaultRecord.setUpdateDateTime(new Date());
        int i = faFaultRecordMapper.updateById(faFaultRecord);
        // set time;
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        String str = updateFaFaultRecordDTO.getDownTime();
//        Date date = null;
//        try {
//            date = dateFormat.parse(str);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        faFaultRecord.setDownTime(updateFaFaultRecordDTO.getDownTime());
        // is_deal ?
        if (faFaultRecord.getIsDeal()==1) {
            var faRepairRecord = new FaRepairRecord();
            faRepairRecord.setId(HandleUtils.UUID32());
            faRepairRecord.setCreator(userDTO.getUsername());
            faRepairRecord.setCreateDateTime(new Date());
            faRepairRecord.setIsSoftDelete(false);
            faRepairRecord.setRepairTime(faFaultRecord.getDownTime());
            faRepairRecord.setRepairUser(userDTO.getUsername());// user id
            faRepairRecord.setFaultRecordId(faFaultRecord.getId());// PHM_Record id;
            faRepairRecord.setRemarks(userDTO.getUsername()+"维修");
            //
            faRepairRecordMapper.insert(faRepairRecord);// debug here, the id is null;
        }
//        int i=1;
        return i == 1 ? RootResponse.success(faFaultRecord_old) : RootResponse.error();
    }

    @Override
    public RootResponse deleteFaFaultRecord(String id, LoginUserDTO userDTO) {
        var faFaultRecord_old = faFaultRecordMapper.selectById(id);
        var wrapper = new LambdaQueryWrapper<FaFaultRecord>();
        wrapper.eq(FaFaultRecord::getId, id);
        wrapper.eq(FaFaultRecord::getIsSoftDelete, false);
        var faFaultRecord = faFaultRecordMapper.selectOne(wrapper);
        if (faFaultRecord== null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        faFaultRecord.setIsSoftDelete(true);
        faFaultRecord.setUpdateDateTime(new Date());
        faFaultRecord.setUpdater(userDTO.getUsername());
        faFaultRecordMapper.updateById(faFaultRecord);
        return RootResponse.success(faFaultRecord_old);
    }


}
